package com.cn.ccp.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

@SuppressWarnings("serial")
public class DownLoadXLSFileServlet extends HttpServlet {
	private Logger log = Logger.getLogger(DownLoadXLSFileServlet.class);
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String type = request.getParameter("type");
		String filename = "";
		if(type.equalsIgnoreCase("student")){
			filename = "学生数据模板"+ ".xls";
		}else if(type.equalsIgnoreCase("training")){
			filename = "试题模板"+ ".xls";
		}else if(type.equalsIgnoreCase("teacher")){
			filename = "教职员工数据模板"+ ".xls";
		}
		String path = System.getProperty("webapp.root")+"model/"+filename ;
		
		
		File file = new File(path);

		if (file.exists()) {
			try {
	            // path是指欲下载的文件的路径。
	            // 取得文件名。
	            // 取得文件的后缀名。
	            //String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

	            // 以流的形式下载文件。
	            InputStream fis = new BufferedInputStream(new FileInputStream(file));
	            byte[] buffer = new byte[fis.available()];
	            fis.read(buffer);
	            fis.close();
	            // 清空response
	            response.reset();
	            // 设置response的Header
	            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO-8859-1"));
	            response.addHeader("Content-Length", "" + file.length());
	            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
	            response.setContentType("application/octet-stream");
	            toClient.write(buffer);
	            toClient.flush();
	            toClient.close();
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
		} else {
			System.out.println("文件不存在:" + path);
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println(" <HEAD><TITLE>错误</TITLE>");
			out.println("</HEAD>");
			out.println(" <BODY>");
			out.println("<p><span class=\"error\">文件不存在！</span></p>");
			out.println(" </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	
}
